import {toast} from "../../../utils/tool";

Component({
    data: {
        name: '',
        password: '',
        show: false,
    },
    methods: {
        open(name, password) {
            this.setData({
                name,
                password,
                show: true,
            })
        },
        // 检测手机型号
        connectWifi: function () {
            //检测手机型号
            wx.getSystemInfo({
                success: res => {
                    let system = '';
                    if (res.platform === 'android') system = parseInt(res.system.substr(8));
                    if (res.platform === 'ios') system = parseInt(res.system.substr(4));
                    if (res.platform === 'android' && system < 6) return toast({title: '手机版本不支持'})
                    if (res.platform === 'ios' && system < 11.2) return toast({title: '手机版本不支持'})
                    //2.初始化 Wi-Fi 模块
                    this.startWifi();
                }
            })
        },
        //初始化 Wi-Fi 模块
        startWifi: function () {
            wx.startWifi({
                success: () => {
                    //请求成功连接Wifi
                    this.Connected();
                },
                fail: () => {
                    toast({title: '接口调用失败'})
                }
            })
        },
        // 连接wifi
        Connected: function () {
            const {name, password} = this.data
            wx.connectWifi({
                SSID: name,
                BSSID: '',
                password,
                success: () => {
                    toast({title: 'wifi连接成功', time: 2000,})
                },
                fail: () => {
                    toast({title: 'wifi连接失败', time: 2000})
                }
            })
        },
        connection() {
            this.connectWifi()
        },
        onClose() {
            this.setData({
                show: false
            })
        },
        copyPass() {
            const {password} = this.data
            wx.setClipboardData({
                data: password,
                success: () => {
                    toast({title: '复制成功'})
                    this.onClose()
                }
            })
        },
    }
});
